package pers.cz.orm.entity.template;

import pers.cz.codegen.codegen.CodeGenerator;
import pers.cz.codegen.dbtools.ColumnInfo;
import pers.cz.codegen.dbtools.MetaOperator;
import pers.cz.codegen.dbtools.TableInfo;
import pers.cz.codegen.describe.*;
import pers.cz.codegen.template.CommentTemplate;
import pers.cz.jefconfig.tools.StringUtils;
import pers.cz.common.annotation.Table;
import pers.cz.common.pub.AbstractEntity;
import pers.cz.common.pub.Field;
import utils.ObjUtils;

import java.io.File;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: fast-sharding-query
 * @description: 生成实体类模板
 * @author: Cheng Zhi
 * @create: 2023-08-13 15:25
 **/
public class EntityCodeGenerator extends CodeGenerator {

    private String tableName = "%";

    public EntityCodeGenerator(String tableName) {
        this.tableName = tableName;
    }

    public EntityCodeGenerator() {

    }

    @Override
    public void generateCode() throws Exception {
        MetaOperator metaOperator = new MetaOperator(tableName);
        for (TableInfo tableInfo : metaOperator.getAllTables()) {
            JavaUnit javaUnit = new JavaUnit(StringUtils.getCamelCase(tableInfo.getTableName().toLowerCase()));
            javaUnit.setIpackage(getIpackage());
            javaUnit.setComment(CommentTemplate.classComment);
            javaUnit.setDescribe(tableInfo.getComment());
            javaUnit.setParent("AbstractEntity");
            javaUnit.addAnnotation("@Table(name = \"" + tableName + "\")");

            javaUnit.addImport(new SimpleClass(Table.class));
            javaUnit.addImport(new SimpleClass(AbstractEntity.class));

            List<String> enumList = new ArrayList<String>();
            for (ColumnInfo columnInfo : tableInfo.getColumnInfos()) {
                String fieldName = ObjUtils.under2camel(columnInfo.getCoulumnName().toLowerCase());
                enumList.add(fieldName);
                String className = StringUtils.getCamelCase(columnInfo.getCoulumnName().toLowerCase());

                IClass simpleClass = new SimpleClass(columnInfo.getColumnType());
                JavaField javaField = new JavaField(fieldName);
                javaField.addReturnType(simpleClass);
                // 为常量添加注释
                javaField.setComment(columnInfo.getComment());
                javaField.setModifer(Modifier.PRIVATE);

                JavaMethod javaMethodSet = new JavaMethod("set" + className);
                javaMethodSet.addContent("this." + fieldName + " = " + fieldName + ";");

                JavaMethodParamter paramter = new JavaMethodParamter();
                paramter.setParamterType(simpleClass);
                javaMethodSet.addParamter(fieldName, paramter);

                JavaMethod javaMethodGet = new JavaMethod("get" + className);
                javaMethodGet.setReturnType(simpleClass);
                javaUnit.addImport(simpleClass);
                javaMethodGet.addContent("return this." + fieldName + ";");
                javaUnit.addMethod(javaMethodSet);
                javaUnit.addMethod(javaMethodGet);
                javaUnit.addField(javaField);
            }

            JavaInnerClazz javaInnerClazz = new JavaInnerClazz("Field", JavaUnit.UnitType.ENUM);
            IClass iClass = new SimpleClass(Field.class);
            javaInnerClazz.addInterface(iClass);
            for (String field : enumList) {
                javaInnerClazz.addEnum(new JavaEnum(field));
            }
            javaUnit.addInnerClazz(javaInnerClazz);
            File testFolder = new File(getFileFolder());
            javaUnit.saveToSrcFolder(testFolder, Charset.forName("UTF8"));
        }
    }

    @Override
    public void enhanceCode() throws Exception {

    }
}
